package com.fmboy.MXH.nav.controller;

import com.fmboy.MXH.nav.domain.ErrorCode;
import com.fmboy.MXH.nav.domain.Result.Result;
import com.fmboy.MXH.nav.domain.dto.NavigationQueryDTO;
import com.fmboy.MXH.nav.domain.pojo.NavigationVO;
import com.fmboy.MXH.nav.service.NavigationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/mxh-nav/navigation")
@Slf4j
@Tag(name = "导航管理", description = "导航管理")
public class NavigationController {
    @Resource
    private NavigationService navigationService;

    //获取-导航
    @GetMapping
    @Operation(summary = "获取-导航", description = "获取-导航")
    public Result getNavigation(
            @RequestParam(required = false) String navigation_name,
            @RequestParam(required = false) Integer category_id,
            @RequestParam(required = false) String state
    ) {
        NavigationQueryDTO navigationQueryDTO = new NavigationQueryDTO();
        navigationQueryDTO.setNavigation_name(navigation_name);
        navigationQueryDTO.setCategory_id(category_id);
        navigationQueryDTO.setState(state);
        try {
            List<NavigationVO> navigation = navigationService.getNavigation(navigationQueryDTO);
            return Result.success("获取导航列表成功！", navigation);
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //获取-导航详情
    @GetMapping("/info")
    @Operation(summary = "获取-导航详情", description = "获取-导航详情")
    public Result getNavigationDetail(@RequestParam Integer id) {
        try {
            NavigationVO navigation = navigationService.getNavigationById(id);
            return Result.success("获取导航详情成功！", navigation);
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //新增-导航
    @PostMapping
    @Operation(summary = "新增-导航", description = "新增-导航")
    public Result addNavigation(@RequestBody NavigationQueryDTO navigationQueryDTO) {
        try {
            navigationService.saveNavigation(navigationQueryDTO);
            return Result.success("新增导航成功！");
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //修改-导航
    @PutMapping
    @Operation(summary = "修改-导航", description = "修改-导航")
    public Result updateNavigation(@RequestBody NavigationQueryDTO navigationQueryDTO) {
        try {
            navigationService.updateNavigationById(navigationQueryDTO);
            return Result.success("修改导航成功！");
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //修改-导航发布状态
    @PutMapping("/state")
    @Operation(summary = "修改-导航发布状态", description = "修改-导航发布状态")
    public Result updateNavigationState(@RequestBody NavigationQueryDTO navigationQueryDTO) {
        try {
            navigationService.updateNavigationState(navigationQueryDTO);
            return Result.success("修改导航发布状态成功！");
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //删除-导航
    @DeleteMapping
    @Operation(summary = "删除-导航", description = "删除-导航")
    public Result deleteNavigation(@RequestParam Integer id) {
        try {
            navigationService.deleteById(id);
            return Result.success("删除导航成功！");
        } catch (RuntimeException e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }
}
